import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/project_same_time_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';

class ThreeSameTimeController extends GetxController {
  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 搜索字符串
  String? search;

  /// 选择的项目类型
  String? projectType;

  /// 选择的当前阶段（状态）
  String? projectNowStatus;

  /// 选择的项目开始时间
  String? projectBeginDate;

  /// 选择的项目结束时间
  String? projectEndDate;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    // 获取后端类型字典
    final List<GetDictAllEntity> dictTypeList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'xmlx_RKiFZp00');
    // 获取后端状态字典
    final List<GetDictAllEntity> statusTypeList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'xmjd_P3nXXsFJ');

    // 添加类型筛选条件
    final List<FilterEntitiesBean> typeList = [];
    for (var item in dictTypeList) {
      typeList.add(FilterEntitiesBean(name: item.dictionaryLabel!, value: item.id!));
    }

    // 添加类型筛选条件
    final List<FilterEntitiesBean> statusList = [];
    for (var item in statusTypeList) {
      statusList.add(FilterEntitiesBean(name: item.dictionaryLabel!, value: item.id!));
    }

    // 添加时间类型
    final List<FilterEntitiesBean> timeList = [];
    timeList.add(FilterEntitiesBean(name: '开始时间', value: projectBeginDate));
    timeList.add(FilterEntitiesBean(name: '结束时间', value: projectEndDate));

    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '项目类型', filterList: typeList);
    final FilterBean filterBean2 = FilterBean(title: '状态', filterList: statusList);
    final FilterBean filterBean3 = FilterBean(title: '起止时间', filterList: timeList);
    filterList.add(filterBean);
    filterList.add(filterBean2);
    filterList.add(filterBean3);
  }

  /// 获取数据
  Future<List<ProjectSameTimeEntity>> getListData(int pageNum, int pageSize) async {
    // 获取列表接口
    final res = await HttpUtil().generateGetRequest<List<ProjectSameTimeEntity>>(
      Interface.getProjectSameTimePage,
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'projectName': search,
        'projectType': projectType,
        'projectNowStatus': projectNowStatus,
        'projectBeginDate': projectBeginDate,
        'projectEndDate': projectEndDate
      },
    ).execute();
    List<ProjectSameTimeEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  } // 获取列表数据

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    projectType = null;
    projectNowStatus = null;
    projectBeginDate = null;
    projectEndDate = null;
    for (var i in value) {
      if (i.title == '起止时间') {
        for (var item in i.filterList) {
          switch (item.name) {
            case '开始时间':
              if (item.value != null && item.value != '') {
                projectBeginDate = item.value as String;
              }
              break;
            case '结束时间':
              if (item.value != null && item.value != '') {
                projectEndDate = item.value as String;
              }
              break;
          }
        }
      }
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '项目类型') {
        projectType = i.chosenValue[0].value as String;
      }
      if (i.title == '状态') {
        projectNowStatus = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  /// 筛选
  void screen() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }
}
